package com.epcs.subGNuevosNegocios.eci.conexion;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLTimeoutException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import com.epcs.subGNuevosNegocios.eci.exception.ConnectionOracleException;

public class DataSourceJndiConection {
	private static DataSourceJndiConection instance;
	private DataSource dataSource;

	private DataSourceJndiConection() {

		try{
			
		    Context ctx = new InitialContext();
		    dataSource = (DataSource) ctx.lookup("ECIBNNMDWMDS");

		} catch (NamingException e) {
		
			throw new ConnectionOracleException("Error de driver de base de datos.",e);
		} catch (Exception e){
			
			throw new ConnectionOracleException("Error no se encuentra servicio de Base de datos.",e);
		}

	}

	public static DataSourceJndiConection getInstance() {
		if(instance==null){
			instance = new DataSourceJndiConection();
		}
		return instance;
	}

	public Connection getConnection() {
		try {
			return dataSource.getConnection();
			
		}catch (SQLTimeoutException e) {

			throw new ConnectionOracleException("Error de tiempo de espera de base de datos.",e);
		} catch (SQLException e) {

			throw new ConnectionOracleException("Error de conexion de base de datos.",e);
		}
	}

}
